#pragma once

#include<iostream>
#include<assert.h>

using namespace std;



enum Color
{
	RED,
	BLACK
};

template<class K,class V>
struct RBTNode
{
	 RBTNode* _left;
	 RBTNode* _right;
	 RBTNode* _parent;
	 Color _col;
	 pair<K, V> _kv;

	 RBTree(const pair<K, V>& kv)
		 :_left(nullptr)
		 ,_right(nullptr)
		 ,_parent(nullptr)
		 ,_kv(kv)
		 ,_col(RED)
	 {}
};

template<class K, class V>
class RBTree
{
	typedef RBTNode<K,V> Node;


public:

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);

		if (parent->_kv.first > cur->_kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent->_col == RED)
		{
			Node* grandparent = parent->_parent;
			if (parent == grandparent->_right)
			{
				Node* uncle = grandparent->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandparent->_col = RED;
				}
				else if (uncle == nullptr || uncle->_col == BLACK)
				{
					if (cur == parent->_left)
					{
						RotatrL(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED; 
					}
					break;
				}
			}
			else if(parent==grandparent->_left)
			{
				Node* uncle = grandparent->_right;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandparent->_col = RED;
				}
				else if (uncle == nullptr || uncle->_col = BLACK)
				{
					if (cur == parent->_left)
					{
						RotateR(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
					}
					else if (cur == parent->_right)
					{
						RotateL(parent);
						RotateR(grandparent);
						grandparent->_col = RED;
						cur->_col = BLACK;
						
					}
				}
				break;
			}

			cur = grandparent;
			parent = cur->_parent;
		}

		_root->_col = BLACK;
		return true;
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_left;
		Node* subRL = subRL;

		parent->_right = subRL;
		subR->_left = parent;

		Node* pparent = parent->_parent;

		parent->_parent = subR;
		if (subRL)
			subRL->_parent = parent;

		if (_root == parent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subR;
			}
			else
			{
				pparent->_right = subR;
			}
			subR->_parent = pparent;
		}
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		Node* pparent = parent->_parent;

		parent->_left = subLR;
		subL->_right = parent;

		parent->_parent = subL;
		if (subLR)
			subLR->_parent = parent;

		if (_root == parent)
		{
			_root = subL;
			subL->_parent;
		}
		else
		{
			if (parent == pparent->_left)
			{
				pparent->_left = subL;
			}
			else
			{
				pparent->_right = subL;
			}
			subL->_parent = pparent;
		}

	}

	void RotateLR(Node* parent)
	{
		Node* pparent = parent->_parent;
		RotateL(parent);
		RotateR(pparent);
	}

	void RotateRL(Node* parent)
	{
		Node* pparent = parent->_parent;
		RotateR(parent);
		RotateL(pparent);
	}

	void Inorder()
	{
		_Inorder(_root);
	}

	void _Inorder(Node* root)
	{
		if (root == nullptr)
			return;
		_Inorder(root->_left);
		cout << root->_kv.first << "  ";
		_Inorder(root->_right);
	}

private:
	Node* _root = nullptr;

};

